﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate;
using NHibernate.Cfg;
using NHibernate.Dialect;
using NHibernate.Driver;
using NHibernate.Connection;
using System.Data;
using NHibernate.Mapping.ByCode;
using TKToXML.Persistence.ClassMaps;

namespace TKToXML.Persistence
{
    public class NHibernateConfiguration
    {
        public static ISessionFactory SessionFactory { get; private set; }
        public static ISession Session
        {
            get
            {
                if (Session == null)
                    return SessionFactory.OpenSession();
                else
                    return Session;
            }
        }

        public static void Initialize(string connectionStringName, Action<Configuration> schemaConfiguration)
        {
            NHibernate.Cfg.Configuration configuration = new NHibernate.Cfg.Configuration();

            configuration.DataBaseIntegration(dbi =>
            {
                dbi.Dialect<MsSql2008Dialect>();
                dbi.Driver<SqlClientDriver>();
                dbi.ConnectionStringName = connectionStringName;// "GuitarStore";
                dbi.ConnectionProvider<DriverConnectionProvider>();
                dbi.IsolationLevel = IsolationLevel.ReadUncommitted;
                dbi.Timeout = 15;
                dbi.BatchSize = 10;
            });

            var modelMapper = new ModelMapper();
            Type[] mappingTypes = typeof(VanDonMap).Assembly.GetExportedTypes()
                .Where(t => t.Name.EndsWith("Map")).ToArray();
            modelMapper.AddMappings(mappingTypes);            
            configuration.AddMapping(modelMapper.CompileMappingForAllExplicitlyAddedEntities());
            //configuration.AddAssembly("TKToXML");
            SessionFactory = configuration.BuildSessionFactory();
            //return configuration;
        }

        public static ISession Initialize(string connectionStringName)
        {
            NHibernate.Cfg.Configuration configuration = new NHibernate.Cfg.Configuration();

            configuration.DataBaseIntegration(dbi =>
            {
                dbi.Dialect<MsSql2008Dialect>();
                dbi.Driver<SqlClientDriver>();
                dbi.ConnectionStringName = connectionStringName;// "GuitarStore";
                dbi.ConnectionProvider<DriverConnectionProvider>();
                dbi.IsolationLevel = IsolationLevel.ReadUncommitted;
                dbi.Timeout = 15;
                dbi.BatchSize = 10;
            });

            var modelMapper = new ModelMapper();
            Type[] mappingTypes = typeof(NHibernateConfiguration).Assembly.GetExportedTypes()
                .Where(t => t.Name.EndsWith("Map")).ToArray();
            modelMapper.AddMappings(mappingTypes);
            configuration.AddMapping(modelMapper.CompileMappingForAllExplicitlyAddedEntities());            
            return configuration.BuildSessionFactory().OpenSession();            
        }

        public static ISession CreateAndOpenSession()
        {
            return SessionFactory.OpenSession();
        }        
    }
}
